﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using pos.entity;
using System.Threading;
using System.Collections;
using NHibernate.Criterion;
namespace pos
{
    public partial class MainFrm : Form
    {
        private Form m_payForm;
        public Form payForm
        {
            get
            {
                return m_payForm;
            }
            set
            {
                m_payForm = value;
            }
        }
        private Form m_quickQuery;
        public Form QuickQueryFrm
        {
            get
            {
                return m_quickQuery;
            }
            set
            {
                m_quickQuery = value;
            }
        }

        private Form m_storageForm;
        public Form storageForm
        {
            get
            {
                return m_storageForm;
            }
            set
            {
                m_storageForm = value;
            }
        }
        private Form m_commodityForm;
        public Form commodityForm
        {
            get
            {
                return m_commodityForm;
            }
            set
            {
                m_commodityForm = value;
            }
        }
        private Form m_saleAdminForm;
        public Form saleAdminForm
        {
            get
            {
                return m_saleAdminForm;
            }
            set
            {
                m_saleAdminForm = value;
            }
        }
        public MainFrm()
        {
            InitializeComponent();
            payForm = null;
        }

        private void cmdAdminStroage_Click(object sender, EventArgs e)
        {
            //  this.Hide();
            if (m_storageForm == null)
                m_storageForm = new StorageFrm(this);
             this.Hide();
            m_storageForm.WindowState = FormWindowState.Maximized;
            m_storageForm.Show();


        }
        private void cmdPay_Click(object sender, EventArgs e)
        {
            this.Hide();
            payFrmLogin dlg = new payFrmLogin();
            DialogResult rs = dlg.ShowDialog();
            if (rs == DialogResult.Yes)
            {
                if (consts.groupID == "admin")
                {
                    this.Show();
                    return;
                }
                // m_paytool.userID = dlg.loginID;
                //  this.Text = "当前收银工号  " + m_paytool.userID;
                if (m_payForm == null)
                    m_payForm = new PayFrm(this);
                m_payForm.WindowState = FormWindowState.Maximized;
                m_payForm.Show();

            }
            else
            {
                this.Close();
                return;
            }

        }
        private void mainForm_Load(object sender, EventArgs e)
        {
            //db_connect.addRef();
            string curDir = Directory.GetCurrentDirectory();
            // string str;
            if (File.Exists(string.Format("{0}\\netverion.txt", curDir)))
            {
              //  db_connect.g_strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=z:\\pos.mdb";
                //  db_connect.g_strCon = "host=192.168.1.5;database=test;uid=root;pwd=root;charset=utf-8";
                this.Text += "--网络版本";
            }
            else
            {
                // db_connect.g_strCon = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}\\pos.mdb", curDir);
              //  db_connect.g_strCon = "server=192.168.1.5;user id=root;Password=root;persist security info=True;database=test";
                this.Text += "--本地版本";


            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
           // MessageBox.Show(entity_build.build_all());

        }

        private void cmdCommodity_Click(object sender, EventArgs e)
        {
            if (m_commodityForm == null)
                m_commodityForm = new CommodityFrm(this);

            m_commodityForm.MdiParent = this;
         //  this.Hide();
            m_commodityForm.WindowState = FormWindowState.Maximized;
            m_commodityForm.Show();

            //    m_commodityForm.Activate();
            //  m_commodityForm.

        }

        private void button2_Click(object sender, EventArgs e)
        {
           // MessageBox.Show(entity_build.drop_all());
        }

        private void MainFrm_Shown(object sender, EventArgs e)
        {
            string curDir = Directory.GetCurrentDirectory();
            //  string str;
            if (!File.Exists(string.Format("{0}\\netverion.txt", curDir)))
            {
                cmdPay_Click(this, null);
            }
            else
            {
                this.Hide();
                payFrmLogin dlg = new payFrmLogin();
                DialogResult rs = dlg.ShowDialog();
                if (rs == DialogResult.Yes)
                {

                    this.Show();
                }
                else
                {
                    this.Close();
                    return;
                }
            }
        }

        private void cmdSale_Click(object sender, EventArgs e)
        {
            if (consts.groupID != "admin")
                return;
            if (m_saleAdminForm == null)
                m_saleAdminForm = new saleAdminFrm(this);
            // this.Hide();
            this.Hide();
            m_saleAdminForm.WindowState = FormWindowState.Maximized;
            m_saleAdminForm.Show();
        }

        private void cmdReturn_Click(object sender, EventArgs e)
        {

        }

        private void cmdHStock_Click(object sender, EventArgs e)
        {
          //  CommodityDealerListFrm dlg = new CommodityDealerListFrm();
           // dlg.WindowState = FormWindowState.Maximized;
           // dlg.ShowDialog();
        }

        private void MainFrm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //    db_connect.releaseRef();
            if (workThread != null)
                workThread.Abort();

        }

        private void QuickQuery_Click(object sender, EventArgs e)
        {
            if (m_quickQuery == null)
                m_quickQuery = new QuickQuery(this);
           this.Hide();
            m_quickQuery.WindowState = FormWindowState.Maximized;
            m_quickQuery.Show();
        }

        private Thread workThread;
        private void cmdKeyInit_Click(object sender, EventArgs e)
        {
            workThread = null;
            return;/*
            workThread = new Thread(new ThreadStart(this.SattelTask));
            workThread.IsBackground = true;
            workThread.Start();
            */


        }
        public void SattelTask()
        {

          /*

            db_connect db = new db_connect();
            db.Open();
            db_transaction transaction = db.beginTransaction();
            try
            {
                SettleTool tool = new SettleTool();
                t_sale sale=new t_sale();
                //DataSet set = sale.query("", "order by seqID");
                //DataTable t = set.Tables[0];
                IList list=sale.List(sale.DC.AddOrder(Order.Asc("seqID")));
                long i=0;

                foreach (Sale s in list)
                {
                //    sale.initByDataRow(r);
                    
                    tool.Setttle(sale.Init(s));
                    

                    i++;
                    
                    set_Text(i.ToString());
                }
                set_Text("正在转移数据.....");
                db.ExeSql("insert into t_hsale select * from t_sale where settleState=1");
                db.ExeSql("delete from t_sale where settleState=1");
                transaction.commit();
                set_Text("结算成功！");
                db.Close();

            }
            catch (Exception ex)
            {

                transaction.rollback();
                db.Close();
                MessageBox.Show(ex.Message);

            }

            */

        }
        delegate void SetTextCallback(string text);
        public void set_Text(String text)
        {

            if (this.cmdKeyInit.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(set_Text);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.cmdKeyInit.Text = text;
            }

        }
      

        public void ThreadTask()
        {

            //MessageBox.Show(idea.tools.StringUtil.GetPYString("一家人葡萄糖-婴儿 700g"));
            t_commodity t = new t_commodity();
            IList list = t.List(null);
            int i = 0;
            foreach (Commodity r in list)
            {
                t.Init(r);
                t.commodityKey = idea.tools.Spell.MakeSpellCode(t.commodityName, idea.tools.SpellOptions.FirstLetterOnly).ToLower();
                t.update();
                set_Text(i.ToString());
                i++;
            }

          

        }

        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {

        }



    }
}